/** 同行同住分析器 */
class CoTravelAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '同行同住';
    }
    async analyze(params = {}) {
        // 简化版：提取住宿记录  
        const sql = `SELECT hotel_name, date(check_in_time) as check_date, COUNT(DISTINCT guest_name) as guest_count, GROUP_CONCAT(DISTINCT guest_name) as guests FROM accommodation_records WHERE case_id = ? GROUP BY hotel_name, check_date HAVING guest_count > 1 ORDER BY guest_count DESC LIMIT 50`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('co_travel', this.name, result.data || [], `发现 ${result.data?.length || 0} 次同住记录`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无同住记录</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">酒店</th><th style="padding:10px; border:1px solid #e0e0e0;">日期</th><th style="padding:10px; border:1px solid #e0e0e0;">人数</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.hotel_name}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.check_date}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.guest_count}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.CoTravelAnalyzer = CoTravelAnalyzer;

/** 隐形房产分析器 */
class HiddenRealEstateAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '隐形房产';
    }
    async analyze(params = {}) {
        const sql = `SELECT hotel_name, COUNT(DISTINCT date(check_in_time)) as visit_count, MIN(check_in_time) as first_visit, MAX(check_in_time) as last_visit FROM accommodation_records WHERE case_id = ? GROUP BY hotel_name HAVING visit_count >= 3 ORDER BY visit_count DESC LIMIT 30`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('hidden_property', this.name, result.data || [], `发现 ${result.data?.length || 0} 个频繁到访地点`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无异常</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">地点</th><th style="padding:10px; border:1px solid #e0e0e0;">到访次数</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.hotel_name}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.visit_count}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.HiddenRealEstateAnalyzer = HiddenRealEstateAnalyzer;

/** 线下会面分析器 */
class OfflineMeetingAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '线下秘密会面';
    }
    async analyze(params = {}) {
        const sql = `SELECT hotel_name, date(check_in_time) as meet_date, COUNT(*) as person_count FROM accommodation_records WHERE case_id = ? GROUP BY hotel_name, meet_date HAVING person_count > 1 LIMIT 30`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('offline_meeting', this.name, result.data || [], `发现 ${result.data?.length || 0} 次可能的线下会面`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无异常</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">地点</th><th style="padding:10px; border:1px solid #e0e0e0;">日期</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.hotel_name}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.meet_date}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.OfflineMeetingAnalyzer = OfflineMeetingAnalyzer;

/** 昼伏夜出分析器 */
class NocturnalAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '昼伏夜出';
    }
    async analyze(params = {}) {
        const sql = `SELECT guest_name, COUNT(*) as night_checkins FROM accommodation_records WHERE case_id = ? AND CAST(strftime('%H', check_in_time) AS INTEGER) >= 22 GROUP BY guest_name ORDER BY night_checkins DESC LIMIT 30`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('nocturnal', this.name, result.data || [], `发现 ${result.data?.length || 0} 个夜间活动记录`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无异常</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">姓名</th><th style="padding:10px; border:1px solid #e0e0e0;">夜间入住次数</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.guest_name}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.night_checkins}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.NocturnalAnalyzer = NocturnalAnalyzer;
